import json
import os

import requests
import yaml
with open(os.getcwd()+"/interface.yml","r") as interface:
    interface_data = yaml.safe_load(interface)
select_path = interface_data["select_path"]
add_path = interface_data["add_path"]
disable_use_path = interface_data["disable_use_path"]
remove_path = interface_data["remove_path"]
select_all = interface_data['select_all']
# 获取当前文件所在目录的绝对路径
current_dir = os.path.dirname(os.path.abspath(__file__))
# 获取根目录的路径（假设根目录为a）
root_dir = os.path.abspath(os.path.join(current_dir, ".."))
# 构建token.yml文件的路径
yaml_file_path = os.path.join(root_dir, "token.yml")
with open(yaml_file_path,"r") as token:
    token_data = yaml.safe_load(token)
url = token_data["url"]
headers = token_data["header"]
class Send:
    s = requests.Session()
    s.headers.update(headers)
    def read_yaml(self,name):
        with open(os.getcwd()+"/"+name,"r",encoding="utf-8") as f:
            f_data = list(yaml.load_all(stream=f,Loader=yaml.FullLoader))
            return f_data
    @staticmethod
    def select_fu_id() ->dict:
        rep = Send.s.get(url=url+select_all)
        rep_json = rep.json()
        return rep_json
    @staticmethod
    def add_organizational(info) ->dict:
        if info["deptType"] == 3:
            rep_new = Send.select_fu_id()
            for deptId in rep_new['data'][0]['children']:
                if deptId['deptType'] == 1:
                    info["parentId"] = deptId['id']
        info_data = json.dumps(info)
        rep = Send.s.post(url=url+add_path,data=info_data).json()
        return rep
    @staticmethod
    def use_dis(info,code) ->dict:
       #禁用结构体
        data = {
            "deptIds": info,
            "deptState": code
        }
        data_json = json.dumps(data)
        rep = Send.s.post(url+disable_use_path,data= data_json).json()
        return rep
    @staticmethod
    def edit_organizational(info):
        data ={
            "parentId": info['parentId'],
            "deptId": info['id'],
            "name": "小昊测试机构",
            "deptType": 1,
            "deptState": "0",
            "openingHoursStart":  "08:30:00",
            "openingHoursEnd": "17:30:00",
            "deptImage": "",
            "nextDay": "",
            "areaCode": "110000,110100,110101",
            "areaName": "北京市西城区",
            "deptAddress": "金融中心"
        }
        info_json = json.dumps(data)
        rep = Send.s.put(url=url+add_path,data=info_json).json()
        return rep
    @staticmethod
    def delect_organizational(info):
        for item in info:
            if item["name"] == "删除机构测试":
                rep = Send.s.delete(url=url+add_path+str(item["id"])).json()
                return rep
            continue
    @staticmethod
    def delect_big_organizational(info):
        arr=[]
        for item in info:
            if item["name"] == "批量删除机构测试":
                arr.append(item['id'])
                rep = Send.s.post(url=url + remove_path,json=arr).json()
                return rep
            continue


